package com.gsoe.judge.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gsoe.base.exception.JudgeException;
import com.gsoe.base.exception.ProblemException;
import com.gsoe.dto.JudgeExamDTO;
import com.gsoe.dto.UpDateScoreDTO;
import com.gsoe.dto.UpdateRegisterUserScoreDTO;
import com.gsoe.judge.mapper.JudgeExamMapper;

import com.gsoe.judge.service.JudgeExamService;
import com.gsoe.po.JudgeExam;
import com.gsoe.vo.PeopleJudgeCacheVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 考试评测表 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@Service
public class JudgeExamServiceImpl extends ServiceImpl<JudgeExamMapper, JudgeExam> implements JudgeExamService {
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public void saveJudgeExam(List<JudgeExamDTO> judgeExamDTO) {
        if(CollectionUtil.isEmpty(judgeExamDTO)){return;}

        Long examId = judgeExamDTO.get(0).getExamId();

        List<JudgeExam> judgeExamList = judgeExamDTO.stream().map(j -> {
            JudgeExam judgeExam = new JudgeExam();
            BeanUtils.copyProperties(j, judgeExam);
            //以下是需要人工批阅的成绩判断
            //如果传过来的score为空 说明该题需要人工判断
            //则获取数据库中的score值，以延续已经人工给分的得分，不至于自动判题后将人工得分清空
            if (judgeExam.getScore() == null) {
                JudgeExam judgeExamInDb = lambdaQuery()
                        .eq(JudgeExam::getExamId, j.getExamId())
                        .eq(JudgeExam::getProblemId, j.getProblemId())
                        .eq(JudgeExam::getUserId, j.getUserId())
                        .one();


                judgeExam.setScore(judgeExamInDb==null?null:judgeExamInDb.getScore());
            }
            return judgeExam;
        }).toList();

        remove(new QueryWrapper<JudgeExam>()
                .eq("exam_id",examId)
        );

        saveBatch(judgeExamList);
    }

    @Override
    public List<PeopleJudgeCacheVO> getPeopleJudgeCache(Long examId, Long problemId) {
        List<JudgeExam> judgeExamList = lambdaQuery()
                .eq(JudgeExam::getExamId, examId)
                .eq(JudgeExam::getProblemId, problemId)
                .list();
        if(CollectionUtil.isEmpty(judgeExamList)){return List.of();}
        return judgeExamList.stream().map(judgeExam -> {
                    PeopleJudgeCacheVO peopleJudgeCacheVO=new PeopleJudgeCacheVO();
                    BeanUtils.copyProperties(judgeExam,peopleJudgeCacheVO);
                    return peopleJudgeCacheVO;
                }).toList();

    }

    @Override
    public void updateScore(UpDateScoreDTO upDateScoreDTO) {
        Long id = upDateScoreDTO.getId();
        Long examId = upDateScoreDTO.getExamId();
        if(id==null || examId==null){throw new ProblemException("updateScore：必要参数缺失");}
        JudgeExam judgeExam = getById(id);
        if(judgeExam==null){throw new ProblemException("updateScore:无此id");}

        UpdateRegisterUserScoreDTO updateRegisterUserScoreDTO=new UpdateRegisterUserScoreDTO();
        BeanUtils.copyProperties(upDateScoreDTO,updateRegisterUserScoreDTO);
        updateRegisterUserScoreDTO.setScore(upDateScoreDTO.getScore()- (judgeExam.getScore()==null?0: judgeExam.getScore()));
        rabbitTemplate.convertAndSend("exam.direct","updateScore",updateRegisterUserScoreDTO);
        judgeExam.setScore(upDateScoreDTO.getScore());
        updateById(judgeExam);

    }
}
